package com.android.iridium;


import java.util.TimeZone;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

public class IridiumFlaresActivity extends Activity {
	
	private static final String PREFS_NAME = "IRFL_PREFS";
	 static final int MAP_REQUEST = 99;
	private EditText latText;
	private EditText longText;
	private EditText altText;
	private Spinner mSpinner;
	LocationManager locationManager;
	private boolean dataSetFromMap;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dataSetFromMap = false;
        latText = (EditText)findViewById(R.id.latText);
        longText = (EditText)findViewById(R.id.longText);
        altText = (EditText)findViewById(R.id.altText);
        mSpinner = (Spinner)findViewById(R.id.timezone_spinner);
     // Acquire a reference to the system Location Manager
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        
    }
    
    public void currentAction(View v){
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
    public void mapAction(View v){
    	Intent myIntent = new Intent(getBaseContext(), IridiumMapActivity.class);
    	startActivityForResult(myIntent, MAP_REQUEST);
    }
    public void loadAction(View v){
    	
    }
    public void saveAction(View v){
    	
    }
    public void submitAction(View v){
    	
    	Intent myIntent = new Intent(getBaseContext(), MyWebView.class);
    	StringBuffer sb = new StringBuffer();
    	sb.append("http://www.heavens-above.com/IridiumFlares.aspx?Dur=1&lat=");
    	sb.append(latText.getText());
    	sb.append("&lng=");
    	sb.append(longText.getText());
    	sb.append("&loc=Unspecified&alt=");
    	sb.append(altText.getText());
    	sb.append("0&tz=EET");
    	myIntent.putExtra("param", sb.toString());
        startActivityForResult(myIntent, 0);
    	
    }
    
    public void next24Action(View v){
    	String [] timezones = getResources().getStringArray(R.array.timezones);      
    	Intent myIntent = new Intent(getBaseContext(), MyWebView.class);
    	StringBuffer sb = new StringBuffer();
    	sb.append("http://www.heavens-above.com/IridiumFlares.aspx?Dur=1&lat=");
    	sb.append(latText.getText());
    	sb.append("&lng=");
    	sb.append(longText.getText());
    	sb.append("&loc=Unspecified&alt=");
    	sb.append(altText.getText());
    	sb.append("0&tz=");
    	sb.append(timezones[mSpinner.getSelectedItemPosition()]);
    	myIntent.putExtra("param", sb.toString());
        startActivityForResult(myIntent, 0);
    	
    }
    public void next7Action(View v){
    	String [] timezones = getResources().getStringArray(R.array.timezones);      
    	Intent myIntent = new Intent(getBaseContext(), MyWebView.class);
    	StringBuffer sb = new StringBuffer();
    	sb.append("http://www.heavens-above.com/IridiumFlares.aspx?Dur=7&lat=");
    	sb.append(latText.getText());
    	sb.append("&lng=");
    	sb.append(longText.getText());
    	sb.append("&loc=Unspecified&alt=");
    	sb.append(altText.getText());
    	sb.append("0&tz=");
    	sb.append(timezones[mSpinner.getSelectedItemPosition()]);
    	Log.d("Iridium", sb.toString());
    	myIntent.putExtra("param", sb.toString());
        startActivityForResult(myIntent, 0);
    	
    }
    public void prev48Action(View v){
    	String [] timezones = getResources().getStringArray(R.array.timezones);      
    	TimeZone tz = TimeZone.getDefault();
    	Intent myIntent = new Intent(getBaseContext(), MyWebView.class);
    	StringBuffer sb = new StringBuffer();
    	sb.append("http://www.heavens-above.com/iridiumdaytime.asp?lat=");
    	sb.append(latText.getText());
    	sb.append("&lng=");
    	sb.append(longText.getText());
    	sb.append("&loc=Unspecified&alt=");
    	sb.append(altText.getText());
    	sb.append("0&tz=");
    	sb.append(timezones[mSpinner.getSelectedItemPosition()]);
    	myIntent.putExtra("param", sb.toString());
        startActivityForResult(myIntent, 0);
    	
    }
    public void daylightAction(View v){
    	String [] timezones = getResources().getStringArray(R.array.timezones);      
    	Intent myIntent = new Intent(getBaseContext(), MyWebView.class);
    	StringBuffer sb = new StringBuffer();
    	sb.append("http://www.heavens-above.com/iridium.asp?Dur=1&lat=");
    	sb.append(latText.getText());
    	sb.append("&lng=");
    	sb.append(longText.getText());
    	sb.append("&loc=Unspecified&alt=");
    	sb.append(altText.getText());
    	sb.append("0&tz=");
    	sb.append(timezones[mSpinner.getSelectedItemPosition()]);
    	myIntent.putExtra("param", sb.toString());
        startActivityForResult(myIntent, 0);
    	
    }
    
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == MAP_REQUEST) {
            if (resultCode == RESULT_OK) {
            	dataSetFromMap = true;
                Location newLoc = new Location(LocationManager.GPS_PROVIDER);
                newLoc.setLongitude(data.getIntExtra("lon", 0) /  1e6);
                newLoc.setLatitude(data.getIntExtra("lat", 0) /  1e6);
            	makeUseOfNewLocation(newLoc);
            	
            }
        }
    }
 
    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

	protected void makeUseOfNewLocation(Location location) {
		
		  latText.setText("" + location.getLatitude());
		  longText.setText("" + location.getLongitude());
		  altText.setText(""+ location.getAltitude());
		  locationManager.removeUpdates(locationListener);
	}
    
	public void onStart(){
		super.onStart();
	       // Restore preferences
	    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		if(!dataSetFromMap){
	       latText.setText(settings.getString("lat", "0"));
	       longText.setText(settings.getString("lng", "0"));
	       altText.setText(settings.getString("alt", "0"));
	       mSpinner.setSelection(settings.getInt("timezone", 41));
		}
		

	}
	public void onStop(){
		dataSetFromMap = false;
	     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	     SharedPreferences.Editor editor = settings.edit();
	     editor.putString("lat", latText.getText().toString());
	     editor.putString("lng", longText.getText().toString());
	     editor.putString("alt", altText.getText().toString());
	     editor.putInt("timezone", mSpinner.getSelectedItemPosition());
	     editor.commit();
		super.onStop();
	}
    
}